package com.example.rjb_shoping.service.impl;

import com.example.rjb_shoping.dao.RjbAdminDao;
import com.example.rjb_shoping.entity.RjbAdmin;
import com.example.rjb_shoping.query.UserQuery;
import com.example.rjb_shoping.service.RjbAdminService;
import com.example.rjb_shoping.util.MailUtils;
import com.example.rjb_shoping.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (RjbAdmin)表服务实现类
 *
 * @author makejava
 * @since 2021-06-09 20:42:20
 */
@Slf4j
@Service("rjbAdminService")
public class RjbAdminServiceImpl implements RjbAdminService, UserDetailsService {
    @Resource
    private RjbAdminDao rjbAdminDao;
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Value("${reset.url}")
    private String url;

    @Override
    public RjbAdmin queryById(Integer aId) {
        return this.rjbAdminDao.queryById(aId);
    }

    @Override
    public List<RjbAdmin> queryAll(UserQuery userQuery) {
        RjbAdmin admin = new RjbAdmin();
        admin.setANickname(userQuery.getName());
        return this.rjbAdminDao.queryAll(admin);
    }

    @Override
    public List<RjbAdmin> likeAll(String username) {
        return rjbAdminDao.likeAll(username);
    }

    @Override
    public RjbAdmin insert(RjbAdmin rjbAdmin) {
        this.rjbAdminDao.insert(rjbAdmin);
        return rjbAdmin;
    }

    @Override
    public RjbAdmin update(RjbAdmin rjbAdmin) {
        rjbAdmin.setPassword(bCryptPasswordEncoder.encode(rjbAdmin.getPassword()));
        int i = this.rjbAdminDao.update(rjbAdmin);
        if (i > 0) {
            return this.queryById(rjbAdmin.getAId());
        }
        return null;
    }

    @Override
    public boolean deleteById(Integer aId) {
        return this.rjbAdminDao.deleteById(aId) > 0;
    }


    @Override
    public Map<String, Object> login(String username, String password) {
//        Jedis jedis = new Jedis();
//        String codes = jedis.get("CHECKCODE_SERVER");
        Map<String, Object> list = new HashMap<>();
        // 判断验证码是否正确，不正确，直接返回
        if (true) {
//        if (codes.equalsIgnoreCase(code)) {
            // password = bCryptPasswordEncoder.encode(password); // 密码加密比对数据库
            try {
                password = Md5Util.encodeByMd5(password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("加密后的密码" + password);
//            RjbAdmin admin = rjbAdminDao.queryByName(username);
//            boolean matches = bCryptPasswordEncoder.matches(admin.getPassword(), password);
            RjbAdmin admin = rjbAdminDao.login(username, password);
            if (admin != null) {
                if (admin.getAEmailStatus().equals("1")) {
                    String status = admin.getAStatus() + "";
                    if (status.equals("1")) {
                        list.put("statis", true);
                        list.put("information", admin);
                    } else {
                        list.put("statis", false);
                        list.put("information", "用户不存在!");
                    }
                } else {
                    list.put("statis", false);
                    list.put("information", "请先查看邮箱激活用户!");
                }
            } else {
                list.put("statis", false);
                list.put("information", "用户名或密码错误!");
            }
        } else {
            list.put("statis", false);
            list.put("information", "验证码错误!");
        }
        return list;
    }

    @Override
    public int register(RjbAdmin rjbAdmin) {
        RjbAdmin admin = rjbAdminDao.queryByName(rjbAdmin.getUsername());
        if (admin == null) {
            try {
                rjbAdmin.setPassword(bCryptPasswordEncoder.encode(rjbAdmin.getPassword()));
                rjbAdmin.setAStatus(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rjbAdminDao.insert(rjbAdmin);
        }
        return -1;
    }

    @Override
    public boolean deleteByIdOne(Integer aId) {
        RjbAdmin rjbAdmin = this.queryById(aId);
        rjbAdmin.setAStatus(-1);
        RjbAdmin updateRjbAdmin = this.update(rjbAdmin);
        return updateRjbAdmin == null;
    }

    @Override
    public boolean selectName(String name) {
        RjbAdmin rjbAdmin = rjbAdminDao.queryByName(name);
        if (rjbAdmin != null) {
            try {
                name = java.net.URLEncoder.encode(name, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String txt = "<a href='http://" + url + "/reset.html?name=" + name + "&id=" + rjbAdmin.getAId() + "'>单击此处重置密码</a>";
            MailUtils.sendMail(rjbAdmin.getAEmai(), txt, "三宝果卉，管理员密码重置邮件");
            return true;
        }
        return false;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        RjbAdmin rjbAdmin = rjbAdminDao.queryByName(username);
        log.error("====" + rjbAdmin);
        if (rjbAdmin == null) {
            throw new UsernameNotFoundException("找不到该用户！");
        }
        return rjbAdmin;
    }
}
